var mysql = require('mysql');
var path = require('path');
var fs = require('fs');

class TMySql {
    constructor() {
        this.config = {
            host: "192.168.1.15",
            port: 23306,
            userName: 'root',
            userPwd: "123",
            sqlPath: path.join(__dirname, "/dbsql")
        };

        this.dbPool = null;
    }
    getPool(callback) {
        var me = this;
        try {
            if (me.dbPool && me.dbPool != null) {
                callback(me.dbPool);
            } else {
                me.dbPool = mysql.createPool({
                    host: '192.168.1.15',
                    port: 23306,
                    user: 'root',
                    password: '123456',
                    database: 'moapp2022',
                    connectionLimit: 100,
                    multipleStatements: true
                });
                callback(me.dbPool);
            }
        } catch (er) {
            console.log(er);
        }
    }
    query(sql, params, callback) {
        var me = this;
        try {
            me.getPool(function(pool) {
                if (pool && pool != null) {
                    pool.getConnection(function(errConn, conn) {
                        if (errConn && errConn != null) {
                            console.log('connect db server faild', err);
                            callback(null);
                        } else {
                            conn.config.queryFormat = function(query, values) {
                                if (!values) return query;
                                return query.replace(/\:(\w+)/g, function(txt, key) {
                                    if (values.hasOwnProperty(key)) {
                                        return this.escape(values[key]);
                                    }
                                    return txt;
                                }.bind(this));
                            };
                            var ps = params || {};
                            conn.query(sql, ps, function(queryError, queryResult) {
                                if (queryError && queryError != null) {
                                    console.log(queryError);
                                    callback(null);
                                } else {
                                    callback(queryResult);
                                }
                            })

                        }
                    })
                } else {
                    callback(null);
                }
            });
        } catch (er) {
            console.log(er);
        }
    }
    queryBySqlFile(sqlFilename, params, callback) {
        var me = this;
        try {
            var fullName = path.join(me.config.sqlPath, sqlFilename);
            if (fullName.indexOf('.txt') < 0) {
                fullName += ".txt";
            }
            if (fs.existsSync(fullName)) {
                //var sql = fs.readFileSync(fullName);
                var sql = fs.readFileSync(fullName, 'utf-8');
                me.query(sql, params, callback);
            } else {
                callback(null);
            }
        } catch (er) {
            console.log(er);
        }
    }
}

var moappDb = new TMySql();
moappDb.config.host = "192.168.1.15";


module.exports = {
    moappDb
}